$(function () {
    $('#myTable').dataTableExtend({
        "ajax": sysInfo.basePath + "/zs/credential/qualification_transfer/get_pending_audit.do",
//			    	"select":true,
        "scrollX": "true",//是否开启横项滚动
        "fixedColumns": {//锁定列，只有开启scrollX时才生效
            "leftColumns": 4,//左侧锁定2列
            // "rightColumns": 1//右侧锁定1列
        },
        "fnServerParams": function (aoData) {
            //搜索条件
            var checkResult = checkHasSybAuth($("#signOrgId option"));
            if (!checkResult) {
                aoData.isAll = "0";
            } else {
                aoData.signOrgId = $("#signOrgId").val();
            }
            aoData.certificateType = $("#certificateType").val();
            aoData.qualificationName = $("#certificateSpecialty").val();
            if ($('[name="certificateLevel"]').val()) {
                aoData.certificateLevel = $('[name="certificateLevel"]').val();
            }
            aoData.overdueStatus = $("#overdueStatus").val();
            aoData.applicationScope = $("#applicationScope").val();
            aoData.certificateArea = $("#certificateAreaCodes").val();
            aoData.keyword = $("#keyword").val();
        },
        "aoColumns": [
            {
                "sClass": "check-th",
                "title": "<input type=\"checkbox\" class=\"checkall\" value=\"undefined\">",
                "orderable": false,
                "data": "ID",
                "render": function (data, type, full, meta) {
                    return '<input type="checkbox"  class="checkchild"  value="' + data + '" />';
                }
            },
            {
                "title": "序号",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return meta.row + 1;
                }
            },
            {
                "title": "关联生产订单",
                "data": "scOrderNo",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    if (!data) {
                        return "";
                    }
                    return "<a href='javascript:void(0);'  class='layui-table-link' onclick='openOrderInfoDetail(\"" + data + "\",\"" + row.scOrderId + "\")'>" + data + "</a>";
                }
            },
            {
                "title": "所属事业部",
                "data": "signOrgName",
                "orderable": false
            },
            {
                "title": "资质编号",
                "data": "certificateNo",
                "orderable": false,
                "render": function (data, type, full, meta) {
                    return "<a href='javascript:void(0);'  class='layui-table-link' onclick='qualificationDetails(\"" + data + "\",\"" + full.baseId + "\")'>" + data + "</a>";
                }
            },
            {
                "title": "资质有效期",
                "data": "certificateExpiryTime",
                "orderable": false,
                "render": function (data) {
                    if (!data) {
                        return "";
                    }
                    return data.substring(0, 10);
                }
            },
            {
                "title": "资质类型",
                "data": "certificateType",
                "orderable": false
            },
            {
                "title": "资质名称",
                "data": "qualificationName",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    data = (null != data && "" != data) ? data : "";
                    return "<div title='" + data + "' class='f-toe' style=\"max-width:100px;word-wrap:break-word\">" + data + "</div>";
                }
            },
            {
                "title": "资质等级",
                "data": "certificateLevel",
                "orderable": false
            },
            {
                "title": "成本价格（元）",
                "data": "receiptPrice",
                "orderable": false,
                "render": function (data) {
                    return fmtMoney(data);
                }
            },
            {
                "title": "转让价格（元）",
                "data": "certificatePrice",
                "orderable": false,
                "render": function (data) {
                    return fmtMoney(data);
                }
            },
            {
                "title": "超期情况",
                "data": "overdueStatus",
                "orderable": false
            },
            {
                "title": "地区",
                "data": "certificateArea",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    areaProvince = (null != row.areaProvince && "" != row.areaProvince) ? row.areaProvince : "";
                    area = (null != row.certificateArea && "" != row.certificateArea) ? "/" + row.certificateArea : "";
                    return areaProvince + area;
                }
            },
            {
                "title": "适用范围",
                "data": "applicationScope",
                "orderable": false
            },
            {
                "title": "操作",
                "orderable": false,
                "data": "id",
                "render": function (data, type, full, meta) {
                    var currentAuditerId = $("#currentAuditerId").val();
                    if (currentAuditerId === full.currentAuditerId) {
                        return '<a onclick="audit(\'' + data + '\')" class="iboss-link"  style="cursor:pointer">审核</a>';
                    }
                    return '-';
                }
            }
        ],
    });

    syb('#signOrgId');
    loadCertificateType('190px', '#certificateType', $("#generalType").val(), 0);
    //异步加载地区
    Plug.plug_ins(
        "get",
        cmsBaseUrl + "/api/cms/tree_book/v1/get_tree_book_list.do?code=2147483647&type=2&level=1",
        "#certificateArea",
        '请选择地区',
        function () {
        },
        true, '2147483647'
    );
});


/**
 * 审核
 * @param id
 */
function audit(id) {
    if (!id) {
        var data = $('#myTable').dataTableExtend('getCheck');
        if (data.length == 0) {
            DGG.Warning('请选择需要审核的资质!');
            return false;
        }
        if (data.length > 1) {
            DGG.Warning('只能单个审核!');
            return false;
        }
        id = data[0].id;
    }
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/credential/qualification_transfer/audit_index.html", {id: id}, function (data) {
        DGG.loading('close');
        var index = dgg.Open({
            type: 1,
            title: '审核',
            content: data,
            area: '600px',
            yes: function () {
                var auditStatus = $("#auditStatus").val();
                var nextReviewer = $("#nextReviewer").select2('val');
                var remark = $("#remark").val();
                if (!auditStatus) {
                    DGG.Warning('请选择审核结果！');
                    return false;
                }
                DGG.loading();
                $.post(sysInfo.basePath + "/zs/credential/qualification_transfer/audit_qualification.do", {
                    id: id,
                    auditStatus: auditStatus,
                    nextReviewer: nextReviewer,
                    remark: remark
                }, function (result) {
                    DGG.loading('close');
                    if (result.code == 0) {
                        DGG.Succeed('审核成功');
                        layer.close(index);
                        searchTable();
                    } else {
                        DGG.Errors(result.msg);
                    }
                })
            }
        })
    }, 'html');
}

/**
 * 变更审核人
 */
function changeAudit() {
    var data = $('#myTable').dataTableExtend('getCheck');
    if (data.length == 0) {
        DGG.Warning('请选择需要变更审核人的资质!');
        return false;
    }
    if (data.length > 1) {
        DGG.Warning('只能单个变更审核人!');
        return false;
    }
    var id = data[0].id;
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/credential/qualification_transfer/change_auditor.html", {id: id}, function (data) {
        DGG.loading('close');
        var index = dgg.Open({
            type: 1,
            title: '审核人变更',
            content: data,
            area: '600px',
            yes: function () {
                var nextReviewer = $("#nextReviewer").select2('val');
                var remark = $("#remark").val();
                if (!nextReviewer) {
                    DGG.Warning('请选择变更审核人！');
                    return false;
                }
                DGG.loading();
                $.post(sysInfo.basePath + "/zs/credential/qualification_transfer/update_auditor.do", {
                    id: id,
                    nextReviewer: nextReviewer,
                    remark: remark
                }, function (result) {
                    DGG.loading('close');
                    if (result.code == 0) {
                        DGG.Succeed('更改审核人成功');
                        layer.close(index);
                        searchTable();

                    } else {
                        DGG.Errors(result.msg);
                    }
                })
            }
        })
    }, 'html');
}

/**
 * 审核设置
 */
function auditStep() {
    var warningType = $("#generalType").val();
    DGG.loading();
    $.post(sysInfo.basePath + "/zs/credential/qualification_transfer/setting_index.html", function (data) {
        DGG.loading('close');
        var index = dgg.Open({
            type: 1,
            title: '审核设置',
            content: data,
            area: '400px',
            yes: function () {
                $.post(sysInfo.basePath + "/zs/warning_configure/update_single_warning.do", {
                    warningType: warningType,
                    warningName: "REVIEW_PROGRESSION",
                    warningValue: $("#reviewSeries").val()
                }, function (result) {
                    if (result.code == 0) {
                        DGG.Succeed('设置成功');
                        layer.close(index);
                        searchTable();
                    } else {
                        DGG.Errors(result.msg);
                    }
                })
            }
        })
    }, 'html');
}

//刷新
function searchTable() {
    $('#myTable').dataTableExtend("reload");
}